<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>Insert title here</title>
	<link rel="stylesheet" href="https://cdn.bootcss.com/layer/3.0.1/skin/default/layer.min.css">
	<link rel="stylesheet" href="../../../layui/css/layui.css">
</head>
<body>
	<div style="margin-top: 15px" id="addsysuser">
		<form class="layui-form" action="">
		
			<div class="layui-form-item">
				<label class="layui-form-label" style="width:90px">账号</label>
				<div class="layui-input-inline">
					<input type="text" name="username" required lay-verify="username"
						placeholder="请输入标题" autocomplete="off" class="layui-input">
				</div>
				<div class="layui-form-mid layui-word-aux">字母开头，允许5-16位字母数字下划线</div>
			</div>

			<div class="layui-form-item">
				<label class="layui-form-label" style="width:90px">姓名</label>
				<div class="layui-input-inline">
					<input type="text" name="name" required lay-verify="name"
						placeholder="请输入标题" autocomplete="off" class="layui-input">
				</div>
				<div class="layui-form-mid layui-word-aux">最多输入10位汉字,字母和数字</div>
			</div>

			<div class="layui-form-item">
				<label class="layui-form-label" style="width:90px">手机号码</label>
				<div class="layui-input-inline">
					<input type="text" name="phone" required lay-verify="phone"
						placeholder="请输入标题" autocomplete="off" class="layui-input">
				</div>
			</div>
			
			<div class="layui-form-item">
				<label class="layui-form-label" style="width:90px">性别</label>
				<div class="layui-input-block">
					<input type="radio" name="sex" value="1" title="男"> 
					<input type="radio" name="sex" value="0" title="女" checked>
				</div>
			</div>

			<div class="layui-form-item">
				<label class="layui-form-label" style="width:90px">启用</label>
				<div class="layui-input-block">
      				<input type="checkbox" name="status" lay-skin="switch" lay-text="启用|禁用" checked value="0">
				</div>
			</div>
			
			<div class="layui-form-item" v-if="is_superadmin==1">
				<label class="layui-form-label" style="width:90px">管理员</label>
				<div class="layui-input-block">
      				<input type="checkbox" name="is_admin" lay-skin="switch" lay-text="是|否" value="0">
				</div>
			</div>

			<div class="layui-form-item">
				<div class="layui-input-block">
					<button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
					<button type="reset" class="layui-btn layui-btn-primary">重置</button>
				</div>
			</div>
		</form>
	</div>

	<!-- jQuery -->
	<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
	<!-- jquery.cookie -->
	<script src="https://cdn.bootcss.com/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
	<!-- layer -->
	<script src="https://cdn.bootcss.com/layer/3.0.1/layer.min.js"></script>
	<!-- vue -->
	<script src="http://cdn.jsdelivr.net/vue/1.0.7/vue.min.js"></script>
	<script src="../../../layui/layui.js"></script>
	<script src="../../../js/byking.js"></script>
	<script src="../../../js/hostconfig.js"></script>
	<script src="../../../js/byking-verify.js"></script>
	<script>
	
	var addsysuser = new Vue({
        el: "#addsysuser",
        data: {
        	is_superadmin:$byking.getCookieValue("is_superadmin")
        }
    });
	
	var guid = $byking.getUrlParam("guid");
	
	var submitflag = true;
  	
	layui.use('form', function() {
		var form = layui.form();
		
		//验证提交
		form.verify({  
			username: function(value) {  
	          if (!$bykingVerify.checkSpecialChar(value)) {
	        	return '字母开头，允许5-16字节，允许字母数字下划线';
	          }
	        }
  			,name: function(value) {
  				if(value.length == 0){  
					return '姓名必须填写';
				}
				if(value.length > 10){  
					return '姓名至多得10个字符';
				}
				if(!$bykingVerify.checkRule1(value)) {
					return '名字只能包括中文、英文、数字包括下划线';
				}
			}
		});
		
		//监听提交
		form.on('submit(formDemo)', function(data) {
			if(!submitflag){
				return false;
			}
			submitflag = false;
			$(".layui-btn:first").attr("disabled","disabled");
			$(".layui-btn").addClass("layui-btn-disabled");
			
			var data = data.field;
			var newData = {
				username: data.username,
				dep_guid: guid,
				name: data.name,
				sex: data.sex,
				phone: data.phone,
				status: 1,
				is_del: "0",
				creator_by : $byking.getCookieValue("user_guid"),
				is_admin: 1
			};
			if(data.status){
				newData.status = data.status;
			}
			if(data.is_admin){
				newData.is_admin = data.is_admin;
			}
			//alert(JSON.stringify(newData));
			$byking.postJSONWithToken($hostconfig.sysAuthHost+"/user/add",newData,function(data){
				layer.msg("新增账户成功!");
				var index = parent.layer.getFrameIndex(window.name);
				parent.refrehUserList();
				parent.layer.close(index);
			},function(data){
				submitflag = true;
				$(".layui-btn:first").removeAttr("disabled"); 
				$(".layui-btn").removeClass("layui-btn-disabled");
				layer.msg(data.responseJSON.message, {icon:5});
			});
			return false;
		});
	});
	</script>
</body>
</html>